var index_tabs;
var index_tabsMenu;

$(function () {
    index_tabs = $('#index_tabs').tabs({
        fit: true,
        border: false,
        onContextMenu: function (e, title) {
            e.preventDefault();
            index_tabsMenu.menu('show', {
                left: e.pageX,
                top: e.pageY
            }).data('tabTitle', title);
        },
        tools: [{
            iconCls: 'icon-arrow_refresh',
            handler: function () {
                var href = index_tabs.tabs('getSelected').panel('options').href;
                if (href) {/*说明tab是以href方式引入的目标页面*/
                    var index = index_tabs.tabs('getTabIndex', index_tabs.tabs('getSelected'));
                    index_tabs.tabs('getTab', index).panel('refresh');
                } else {/*说明tab是以content方式引入的目标页面*/
                    var panel = index_tabs.tabs('getSelected').panel('panel');
                    var frame = panel.find('iframe');
                    try {
                        if (frame.length > 0) {
                            for (var i = 0; i < frame.length; i++) {
                                frame[i].contentWindow.document.write('');
                                frame[i].contentWindow.close();
                                frame[i].src = frame[i].src;
                            }
                            if (navigator.userAgent.indexOf("MSIE") > 0) {// IE特有回收内存方法
                                try {
                                    CollectGarbage();
                                } catch (e) {
                                }
                            }
                        }
                    } catch (e) {
                    }
                }
            }
        }, {
            iconCls: 'icon-cancel',
            handler: function () {
                var index = index_tabs.tabs('getTabIndex', index_tabs.tabs('getSelected'));
                var tab = index_tabs.tabs('getTab', index);
                if (tab.panel('options').closable) {
                    index_tabs.tabs('close', index);
                } else {
                    $.messager.alert('提示', '[' + tab.panel('options').title + ']不可以被关闭！', 'error');
                }
            }
        }]
    });


    index_tabsMenu = $('#index_tabsMenu').menu({
        onClick: function (item) {
            var curTabTitle = $(this).data('tabTitle');
            var type = $(item.target).attr('title');

            if (type === 'refresh') {
                index_tabs.tabs('getTab', curTabTitle).panel('refresh');
                return;
            }

            if (type === 'close') {
                var t = index_tabs.tabs('getTab', curTabTitle);
                if (t.panel('options').closable) {
                    index_tabs.tabs('close', curTabTitle);
                }
                return;
            }

            var allTabs = index_tabs.tabs('tabs');
            var closeTabsTitle = [];

            $.each(allTabs, function () {
                var opt = $(this).panel('options');
                if (opt.closable && opt.title != curTabTitle && type === 'closeOther') {
                    closeTabsTitle.push(opt.title);
                } else if (opt.closable && type === 'closeAll') {
                    closeTabsTitle.push(opt.title);
                }
            });

            for (var i = 0; i < closeTabsTitle.length; i++) {
                index_tabs.tabs('close', closeTabsTitle[i]);
            }
        }
    });


    $("#menuTree").tree({
        url: encodeURI(contextRootPath + "/R_Views/emergency/js/tree_data1.json"),
        lines: true,
        animate: true,
        onClick: function (node) {
            alert(node.url);
            var params = {}
            params.id = node.id;
            params.title = node.text;
            params.url = contextRootPath + node.attributes.url;
            params.iconCls = node.attributes.iconCls;
            addTab(params);
        }
    });
});

function addTaba(params) {

    var iframeHeight = $(window).height() - $(".north_header").height() - 60;
    var iframe = '<iframe src="' + params.url + '" frameborder="0" style="border:0;width:100%;height:98%;" ></iframe>';
    // if http url  jia js close
    if (params.url && params.url.indexOf('http') == 0) {
        iframe = '<iframe src="' + params.url + '" frameborder="0" style="border:0;width:100%;height:98%;" security="restricted" sandbox="" ></iframe>';
    }

    var t = $('#index_tabs');
    var opts = {
        title: params.title,
        closable: true,
        iconCls: params.iconCls,
        content: iframe,
        border: false,
        fit: true
    };
    if (t.tabs('exists', opts.title)) {
        t.tabs('close', opts.title);
        t.tabs('add', opts);
    } else {
        t.tabs('add', opts);
    }
}

function addTab(params) {
    var iframeHeight = $(window).height() - $(".north-bg").height() - 40;

    var tabs = $("#index_tabs");
    if (tabs.tabs('exists', params.title)) {
        tabs.tabs('select', params.title);
        var tab = tabs.tabs('getSelected');
        if (tab && tab.find('iframe').length > 0) {
            var _refresh_iframe = tab.find('iframe')[0];
            var refresh_url = params.url ? params.url : _refresh_iframe.src;
            _refresh_iframe.contentWindow.location.href = refresh_url;
        }
    } else {
        var content;
        var scroll = false;
        if (params.url && params.url.indexOf('#') < 0) {
            content = '<iframe class="autoHeight" frameborder="0" scrolling="no"  src="' + params.url
                + '" style="width:100%;height:' + iframeHeight + 'px;"></iframe>';
            tabs.tabs('add', {
                title: params.title,
                id: params.id,
                content: content,
                iconCls: params.iconCls,
                closable: true,
                fit: true
            });
        } else {
            //console.info(params);
        }

    }
}
